package leetcode;

import java.util.ArrayList;
import java.util.List;

public class LeetCode46 {

    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        int[] dict = new int[nums.length];
        calculation(nums, dict, new ArrayList<>());
        return result;
    }

    private void calculation(int[] nums, int[] dict, List<Integer> lastResult) {
        int sum = 0;
        for (int i = 0; i < dict.length; i++) {
            if (dict[i] == 0)
                break;
            sum += dict[i];
        }
        if (sum == dict.length) {
            result.add(lastResult);
            return;
        }
        for (int i = 0; i < dict.length; i++) {
            if (dict[i] == 0) {
                int[] clone = dict.clone();
                clone[i] = 1;
                List<Integer> list = new ArrayList<>(lastResult);
                list.add(nums[i]);
                calculation(nums, clone, list);
            }
        }
    }
//------------------------------------------------------------------
}
